﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.VillageStateChangedLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class VillageStateChangedLogEntry : LogEntry, IWarLog
  {
    [SaveableField(340)]
    public readonly Village Village;
    [SaveableField(341)]
    public readonly Village.VillageStates OldState;
    [SaveableField(342)]
    public readonly Village.VillageStates NewState;
    [SaveableField(343)]
    public readonly IFaction RaiderPartyMapFaction;
    [SaveableField(344)]
    public readonly IFaction VillageRealmWhenRaided;
    [SaveableField(345)]
    public readonly bool RealmsAtWarWhenRaided;
    [SaveableField(346)]
    private readonly MBReadOnlyDictionary<Hero, short> _witnesses;
    [SaveableField(347)]
    public readonly Hero RaidLeader;

    internal static void AutoGeneratedStaticCollectObjectsVillageStateChangedLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Village);
      collectedObjects.Add((object) this.RaiderPartyMapFaction);
      collectedObjects.Add((object) this.VillageRealmWhenRaided);
      collectedObjects.Add((object) this.RaidLeader);
      collectedObjects.Add((object) this._witnesses);
    }

    internal static object AutoGeneratedGetMemberValueVillage(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).Village;
    }

    internal static object AutoGeneratedGetMemberValueOldState(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).OldState;
    }

    internal static object AutoGeneratedGetMemberValueNewState(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).NewState;
    }

    internal static object AutoGeneratedGetMemberValueRaiderPartyMapFaction(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).RaiderPartyMapFaction;
    }

    internal static object AutoGeneratedGetMemberValueVillageRealmWhenRaided(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).VillageRealmWhenRaided;
    }

    internal static object AutoGeneratedGetMemberValueRealmsAtWarWhenRaided(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).RealmsAtWarWhenRaided;
    }

    internal static object AutoGeneratedGetMemberValueRaidLeader(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o).RaidLeader;
    }

    internal static object AutoGeneratedGetMemberValue_witnesses(object o)
    {
      return (object) ((VillageStateChangedLogEntry) o)._witnesses;
    }

    public VillageStateChangedLogEntry(
      Village village,
      Village.VillageStates oldState,
      Village.VillageStates newState,
      MobileParty raiderParty)
    {
      this.Village = village;
      this.OldState = oldState;
      this.NewState = newState;
      this.RaiderPartyMapFaction = raiderParty.MapFaction;
      this.VillageRealmWhenRaided = village.Settlement.MapFaction;
      this.RealmsAtWarWhenRaided = this.RaiderPartyMapFaction.IsAtWarWith(this.VillageRealmWhenRaided);
      this.RaidLeader = raiderParty.LeaderHero;
      if (raiderParty.LeaderHero != Hero.MainHero)
        return;
      Dictionary<Hero, short> dictionary = new Dictionary<Hero, short>();
      foreach (TroopRosterElement troopRosterElement in (List<TroopRosterElement>) MobileParty.MainParty.MemberRoster.GetTroopRoster())
      {
        if (troopRosterElement.Character.HeroObject != null && !dictionary.ContainsKey(troopRosterElement.Character.HeroObject))
          dictionary.Add(troopRosterElement.Character.HeroObject, (short) 1);
      }
      this._witnesses = dictionary.GetReadOnlyDictionary<Hero, short>();
    }

    public bool IsRelatedToWar(StanceLink stance, out IFaction effector, out IFaction effected)
    {
      IFaction faction1 = stance.Faction1;
      IFaction faction2 = stance.Faction2;
      effector = this.RaiderPartyMapFaction?.MapFaction;
      effected = this.VillageRealmWhenRaided?.MapFaction;
      if (this.NewState != Village.VillageStates.Looted)
        return false;
      if (this.VillageRealmWhenRaided == faction1 && this.RaiderPartyMapFaction == faction2)
        return true;
      return this.VillageRealmWhenRaided == faction2 && this.RaiderPartyMapFaction == faction1;
    }

    public override string ToString()
    {
      TextObject textObject = new TextObject("{=!}Village named {SETTLEMENT} state changed from {OLD_STATE} to {NEW_STATE}");
      textObject.SetTextVariable("SETTLEMENT", this.Village.Settlement.Name);
      textObject.SetTextVariable("OLD_STATE", this.OldState.ToString());
      textObject.SetTextVariable("NEW_STATE", this.NewState.ToString());
      return textObject.ToString();
    }

    public override int GetAsRumor(Settlement talkSettlement, ref TextObject comment)
    {
      int asRumor = 0;
      if (this.NewState == Village.VillageStates.Looted && this.Village.Settlement.MapFaction == talkSettlement.MapFaction)
      {
        comment = new TextObject("{=nV1xbbM6}So I hear {ENEMY_NAME} burned and pillaged {VILLAGE_NAME}. Bastards...");
        comment.SetTextVariable("ENEMY_NAME", FactionHelper.GetTermUsedByOtherFaction(this.RaiderPartyMapFaction, talkSettlement.MapFaction, false));
        comment.SetTextVariable("VILLAGE_NAME", this.Village.Name);
        asRumor = 10;
      }
      return asRumor;
    }

    public override void GetConversationScoreAndComment(
      Hero talkTroop,
      bool findString,
      out string comment,
      out ImportanceEnum score)
    {
      short num;
      if (this._witnesses == null || !this._witnesses.TryGetValue(talkTroop, out num))
        num = (short) -2;
      score = ImportanceEnum.Zero;
      comment = "";
      if (talkTroop.MapFaction != Hero.MainHero.MapFaction && talkTroop.MapFaction == this.VillageRealmWhenRaided && this.Village.Settlement.OwnerClan == talkTroop.Clan && talkTroop.Clan.Leader == talkTroop && this.RaidLeader == Hero.MainHero)
      {
        score = ImportanceEnum.Important;
        comment = "str_comment_you_raided_my_village";
        MBTextManager.SetTextVariable("SETTLEMENT_NAME", this.Village.Name, false);
      }
      else if (num == (short) 1 && this.NewState == Village.VillageStates.BeingRaided && this.Village.Settlement.Culture == talkTroop.Culture && this.RaiderPartyMapFaction.Culture != talkTroop.Culture)
      {
        score = ImportanceEnum.QuiteImportant;
        comment = "str_comment_we_raided_my_people";
        MBTextManager.SetTextVariable("VILLAGE_NAME", this.Village.Name, false);
      }
      else if (num == (short) 1 && this.NewState == Village.VillageStates.BeingRaided && this.RaiderPartyMapFaction.MapFaction.IsKingdomFaction && this.RealmsAtWarWhenRaided)
      {
        score = ImportanceEnum.QuiteImportant;
        comment = "str_comment_we_raided_enemy_village";
        MBTextManager.SetTextVariable("VILLAGE_NAME", this.Village.Name, false);
        MBTextManager.SetTextVariable("ENEMY_NAME", this.VillageRealmWhenRaided.InformalName, false);
      }
      else
      {
        if (num != (short) 1 || this.NewState != Village.VillageStates.BeingRaided || this.RealmsAtWarWhenRaided)
          return;
        score = ImportanceEnum.QuiteImportant;
        comment = "str_comment_we_raided_neutral_village";
        MBTextManager.SetTextVariable("VILLAGE_NAME", this.Village.Name, false);
        MBTextManager.SetTextVariable("VILLAGE_FACTION", this.VillageRealmWhenRaided.InformalName, false);
      }
    }
  }
}
